package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.PermissionDao;
import com.chunfeng.mypen.entity.Permission;
import com.chunfeng.mypen.properties.SystemProperties;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 权限业务层实现
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/18
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDao, Permission> implements PermissionService {

    /**
     * 权限Dao注入
     */
    @Autowired
    private PermissionDao permissionDao;

    /**
     * 系统配置
     */
    @Autowired
    private SystemProperties systemProperties;

    /**
     * 分类查询权限
     *
     * @param current    当前页
     * @param size       每页长度
     * @param permission 分类
     * @return JSON
     */
    @Override
    @Cacheable(value = "Permission_Table", key = "#args")
    public JsonRequest<List<Permission>> lookPermission(Integer current, Integer size, Permission permission) {
        Page<Permission> permissionPage = permissionDao.selectPermission(new Page<>(current, size), permission);
        //拿到列表值
        List<Permission> permissionList = permissionPage.getRecords();
        if (permissionList.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        Map<String, Object> page = new HashMap<>();
        //存储总页数
        page.put(systemProperties.getPageKeyName(), permissionPage.getTotal());
        return JsonRequest.success(permissionList, page);
    }

    /**
     * 根据单个路由的详细信息查询该路由对应的权限信息
     *
     * @param uri    请求路径
     * @param method 请求方法
     * @return JSON
     */
    @Override
    @Cacheable(value = "Permission_Router", key = "#args")
    public JsonRequest<List<Permission>> lookPermissionOfOneRouter(String uri, String method) {
        List<Permission> permissions = permissionDao
                .selectPermissionByRouter(uri, method);
        if (permissions.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(permissions);
    }

    /**
     * 根据单个用户信息查看一个
     *
     * @param id 用户名
     * @return JSON
     */
    @Override
    @Cacheable(value = "Permission_User", key = "#args")
    public JsonRequest<List<Permission>> lookPermissionOfOneUser(String id) {
        List<Permission> permissions = permissionDao.selectPermissionByUser(id);
        if (permissions.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(permissions);
    }

    /**
     * 根据单个角色信息查看一个
     *
     * @param rid 角色ID
     * @return JSON
     */
    @Override
    @Cacheable(value = "Permission_Role", key = "#args")
    public JsonRequest<List<Permission>> lookPermissionOfOneRole(String rid) {
        List<Permission> permissionList = permissionDao.selectPermissionByRole(rid);
        if (permissionList.isEmpty()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        return JsonRequest.success(permissionList);
    }

    /**
     * 新增一条权限数据
     *
     * @param permission 权限信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = {
            "Permission_Table",
            "Permission_Router",
            "Permission_User",
            "Permission_Role"})
    public JsonRequest<Integer> insertOnePermission(Permission permission) {
        int column = permissionDao.insert(permission);
        if (column < 1) {
            return JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 修改一条权限数据
     *
     * @param permission 待修改的权限信息
     * @return JSON
     */
    @Override
    @CacheEvict(value = {
            "Permission_Table",
            "Permission_Router",
            "Permission_User",
            "Permission_Role"})
    public JsonRequest<Integer> updateOnePermission(Permission permission) {
        Permission permission1 = permissionDao.selectById(permission.getId());
        //判空
        if (Objects.isNull(permission1)) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = permissionDao.insert(permission);
        if (column < 1) {
            return JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(column);
    }

    /**
     * 批量删除权限
     *
     * @param permissionId 权限ID
     * @return JSON
     */
    @Override
    @CacheEvict(value = {
            "Permission_Table",
            "Permission_Router",
            "Permission_User",
            "Permission_Role"})
    public JsonRequest<Integer> deletePermissions(List<String> permissionId) {
        Integer size = permissionDao.selectPermissionCounts(permissionId);
        //查出来的与数据库不一致
        if (size != permissionId.size()) {
            return JsonRequest.error(RequestException.NOT_FOUND);
        }
        int column = permissionDao.deleteBatchIds(permissionId);
        if (column < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(column);
    }
}
